<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>牛皮小系统</title>
    <!-- 引入通用的js, css等 -->
    <jsp:include page="${request.contextPath}/static/include.jsp" flush="true" />
</head>
<body>
    <div style="padding: 20px; background-color: #F2F2F2;">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-header">示例一：普通的计算</div>
                    <div class="layui-card-body">
                        公式：<input id="formula1" type="text" value="((a+b)*(c+b))/(c+a)/b" class="layui-input">
                        参数：<input id="param1" type="text" value="a=16; b=5; c=66" class="layui-input">
                        <button id="btn1" type="button" class="layui-btn layui-btn-normal" style="margin-top:10px;">计算</button>
                    </div>
                </div>
            </div>
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-header">示例二：判断字符串是否相同</div>
                    <div class="layui-card-body">
                        公式：<input id="formula2" type="text" value='"hello" == "hello"' class="layui-input">
                        <button id="btn2" type="button" class="layui-btn layui-btn-normal" style="margin-top:10px;">计算</button>
                    </div>
                </div>
            </div>
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-header">示例三：参数跟公式写在一起</div>
                    <div class="layui-card-body">
                        参数与公式：<input id="formula3" type="text" value="x=1; y=1.5; z=2; x*y" class="layui-input">
                        <div>备注说明：参数之间用“;”进行分隔，并且公式要写在最后</div>
                        <button id="btn3" type="button" class="layui-btn layui-btn-normal" style="margin-top:10px;">计算</button>
                    </div>
                </div>
            </div>
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-header">示例四：使用中文来表达公式</div>
                    <div class="layui-card-body">
                        公式：<input id="formula4" type="text" value="日薪 * 天数" class="layui-input">
                        参数：<input id="param4" type="text" value="日薪=100; 天数=22;" class="layui-input">
                        <button id="btn4" type="button" class="layui-btn layui-btn-normal" style="margin-top:10px;">计算</button>
                    </div>
                </div>
            </div>
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-header">示例五：三元表达式</div>
                    <div class="layui-card-body">
                        公式：<input id="formula5" type="text" value='"hello" == "hello" ? "相同" : "不相同" ' class="layui-input">
                        <button id="btn5" type="button" class="layui-btn layui-btn-normal" style="margin-top:10px;">计算</button>
                    </div>
                </div>
            </div>

        </div>
    </div>
</body>
</html>

<script>
    layui.use(['form', 'upload'], function(){
        var $ = layui.jquery,
            form = layui.form
            ,layer = layui.layer;

        $("#btn1").click(function(){
            let formula = $("#formula1").val()
                param1 = $("#param1").val();
            let requestParam = {formula : formula}
                ,param = {};
            for(let _param of param1.split(";")){
                let index = _param.indexOf("=");
                if(-1 == index) continue;

                let key = _param.substring(0, index);
                let value = _param.substring(index+1);
                eval(`param.\${key} = \${value}`);
            }

            requestParam.param = JSON.stringify(param);
            $.post(`${__ctxPath}/framework/module/Jep.do?method=parse`, requestParam, function(response){
                if(response.code != RESPONSE_SUCCESS){
                    return layer.alert(response.msg);
                }
                layer.alert(`计算结果为：<b>\${response.data}</b>`, {
                    btn: ['知道啦']
                });
            }, "json");
        });

        $("#btn2").click(function(){
            let formula = $("#formula2").val();
            let requestParam = {formula : formula};
            $.post(`${__ctxPath}/framework/module/Jep.do?method=parse`, requestParam, function(response){
                if(response.code != RESPONSE_SUCCESS){
                    return layer.alert(response.msg);
                }
                layer.alert(`计算结果为：<b>\${response.data}</b>`, {
                    btn: ['知道啦']
                });
            }, "json");
        });

        $("#btn3").click(function(){
            let formula = $("#formula3").val();
            let requestParam = {formula : formula};
            $.post(`${__ctxPath}/framework/module/Jep.do?method=multiParse`, requestParam, function(response){
                if(response.code != RESPONSE_SUCCESS){
                    return layer.alert(response.msg);
                }
                layer.alert(`计算结果为：<b>\${response.data}</b>`, {
                    btn: ['知道啦']
                });
            }, "json");
        });

        $("#btn4").click(function(){
            let formula = $("#formula4").val()
            param1 = $("#param4").val();
            let requestParam = {formula : formula}
                ,param = {};
            for(let _param of param1.split(";")){
                let index = _param.indexOf("=");
                if(-1 == index) continue;

                let key = _param.substring(0, index);
                let value = _param.substring(index+1);
                eval(`param.\${key} = \${value}`);
            }

            requestParam.param = JSON.stringify(param);
            $.post(`${__ctxPath}/framework/module/Jep.do?method=parse`, requestParam, function(response){
                if(response.code != RESPONSE_SUCCESS){
                    return layer.alert(response.msg);
                }
                layer.alert(`计算结果为：<b>\${response.data}</b>`, {
                    btn: ['知道啦']
                });
            }, "json");
        });

        $("#btn5").click(function(){
            let formula = $("#formula5").val();
            let requestParam = {formula : formula};
            $.post(`${__ctxPath}/framework/module/Jep.do?method=parse`, requestParam, function(response){
                if(response.code != RESPONSE_SUCCESS){
                    return layer.alert(response.msg);
                }
                layer.alert(`计算结果为：<b>\${response.data}</b>`, {
                    btn: ['知道啦']
                });
            }, "json");
        });




    });

</script>





























